<?php
namespace User\Controller;

use Common\Controller\AdminbaseController;

class IndexadminController extends AdminbaseController {

	protected $users_model;

	function _initialize() {
		C("SP_ADMIN_DEFAULT_THEME","tciocEx");
		parent::_initialize();
		$this->users_model=M("Users");
	}

	// 后台本站用户列表
    public function index(){
        $where=array();
        $request=I('request.');
        
        if(!empty($request['uid'])){
            $where['id']=intval($request['uid']);
        }
        
        if(!empty($request['keyword'])){
            $keyword=$request['keyword'];
            $keyword_complex=array();
            $keyword_complex['user_login']  = array('like', "%$keyword%");
            $keyword_complex['user_nicename']  = array('like',"%$keyword%");
            $keyword_complex['user_email']  = array('like',"%$keyword%");
            $keyword_complex['_logic'] = 'or';
            $where['_complex'] = $keyword_complex;
        }
        
    	$users_model=M("Users");
    	
    	$count=$users_model->where($where)->count();
    	$page = $this->page($count, 20);
    	
    	$list = $users_model
    	->where($where)
    	->order("create_time DESC")
    	->limit($page->firstRow . ',' . $page->listRows)
    	->select();
    	
    	$this->assign('list', $list);
    	$this->assign("page", $page->show('Admin'));
    	
    	$this->display(":index");
    }
    
    // 后台本站用户禁用
    public function ban(){
    	$id= I('get.id',0,'intval');
    	if ($id) {
    		$result = M("Users")->where(array("id"=>$id,"user_type"=>2))->setField('user_status',0);
    		if ($result) {
				tc_access_log("会员拉黑成功！","admin");
    			$this->success("会员拉黑成功！", U("indexadmin/index"));
    		} else {
				tc_access_log("会员拉黑失败！","admin");
    			$this->error('会员拉黑失败,会员不存在,或者是管理员！');
    		}
    	} else {
    		$this->error('数据传入失败！');
    	}
    }
    
    // 后台本站用户启用
    public function cancelban(){
    	$id= I('get.id',0,'intval');
    	if ($id) {
    		$result = M("Users")->where(array("id"=>$id,"user_type"=>2))->setField('user_status',1);
    		if ($result) {
				tc_access_log("会员启用成功！","admin");
    			$this->success("会员启用成功！", U("indexadmin/index"));
    		} else {
				tc_access_log("会员启用失败！","admin");
    			$this->error('会员启用失败！');
    		}
    	} else {
    		$this->error('数据传入失败！');
    	}
    }

	//	查询后台会员信息
	public function edit(){
		C("SP_ADMIN_DEFAULT_THEME","tciocEx");
		$u_id=$_GET['id'];
		$users = M("Users")->where(['id'=>$u_id])->find();
		$this->assign('users', $users);
		$this->display(":edit");
	}


	public function edit_post(){
		C("SP_ADMIN_DEFAULT_THEME","tciocEx");
		if(IS_POST){
			if (empty($_POST['user_nicename'])) {
				$this->error("昵称不能为空！");
			}
			if (empty($_POST['user_email'])) {
				$this->error("邮箱不能为空！");
			}
			$create_result=$this->users_model
				->field("user_login,create_time,last_login_time,last_login_ip,user_activation_key,role_id,score,user_type",true)//排除相关字段
				->create();
			if ($create_result) {
				if ($this->users_model->save()!==false) {
					tc_access_log("修改会员资料成功！","admin");
					$this->success("保存成功！");
				} else {
					tc_access_log("修改会员资料失败！","admin");
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->users_model->getError());
			}
		}

	}




}
